package com.ymatou.iapi.optrade.testcase;

import java.util.Date;

import org.joda.time.DateTime;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.req.DelayReceiveReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.DelayReceiveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptPo;
import com.ymttest.database.sharding.db.sharding.model.ExtendReceivePo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 延长收货时间 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_DelayReceive {

	private static DelayReceiveReq delayReceiveReq;
	private static DelayReceiveCall delayReceiveCall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("延长收货时间");
	}

	@Before
	public void caseUp() {

		delayReceiveReq = new DelayReceiveReq();
		delayReceiveCall = new DelayReceiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(delayReceiveCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify(Date newDate) {

		long orderId = delayReceiveReq.getOrderId();

		ExtendReceivePo actual = orderShardingWapper
				.selectExtendReceiveByOrder(orderId).get(0);

		Logger.comment(String.format("验证查询延长收货表（extend_receive）  orderId: %s",
				orderId));

		/*		Logger.verifyEquals(expected.getAddTime(), actual.getAddTime(),
						"验证数据库AddTime");
				Logger.verifyEquals(expected.getUpdateTime(), actual.getUpdateTime(),
						"验证数据库UpdateTime");*/

		Logger.verifyEquals(orderId, actual.getOrderId(), "验证数据库OrderId");
		Logger.verifyEquals(delayReceiveReq.getOperateUserType(),
				actual.getBizType(), "验证数据库BizType");
		Logger.verifyEquals(delayReceiveReq.getDelayDay(),
				actual.getExtendDays(), "验证数据库ExtendDays");
		Logger.verifyEquals(delayReceiveReq.getOperateUserId(),
				actual.getOperator(), "验证数据库Operator");

		verifyAutoReceive(orderId, newDate);
	}
	/**
	 * 校验自动确认收货时间操作表（auto_receive_opt） 
	 */
	private void verifyAutoReceive(long orderId, Date newDate) {

		AutoReceiveOptPo autoReceiveOptPo = orderShardingWapper
				.selectAutoReceiveOptByOrderId(orderId);

		Logger.comment(String.format(
				"验证自动确认收货时间操作表（auto_receive_opt）  orderId: %s", orderId));

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(autoReceiveOptPo.getAddTime()) < 2,
				"验证数据库AddTime跟系统当前时间差为2分钟内");
		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(autoReceiveOptPo.getUpdateTime()) < 2,
				"验证数据库UpdateTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, autoReceiveOptPo.getOrderId(),
				"验证数据库OrderId");
		Logger.verifyEquals(false, autoReceiveOptPo.getFreeze(), "验证数据库Freeze");

		Logger.verifyEquals(true, Utils.daysBetween(newDate,
				autoReceiveOptPo.getExpectReceiveTime()) == delayReceiveReq
				.getDelayDay(), "验证数据库ExpectReceiveTime");

	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DelayReceive_001() {
		Logger.start(true, "买家延长收货时间成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			long userId = placeOrderResp.getUserId();

			//贝海发货
			TradingCallService.delivery(sellerId, orderId, true);

			//自动确认收货时间操作表（auto_receive_opt） 
			AutoReceiveOptPo record = new AutoReceiveOptPo();
			record.setOrderId(orderId);
			record.setExpectReceiveTime(DateTime.now().plusDays(-1).toDate());
			record.setFreeze(false);

			//修改预期自动确认收货
			orderShardingWapper.autoReceiveOptMapper
					.updateByPrimaryKeySelective(record);

			Logger.comment("前置数据准备结束");

			int delayDay = 3;

			delayReceiveReq.setDelayDay(delayDay);
			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(userId);

			delayReceiveReq.setOperateUserType(0);
			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, delayReceiveCall.getCode(),
					"验证Code");
			// 数据库验证

			verify(record.getExpectReceiveTime());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_002() {
		Logger.start(false, "客服延长收货成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			//贝海发货
			TradingCallService.delivery(sellerId, orderId, true);

			//自动确认收货时间操作表（auto_receive_opt） 
			AutoReceiveOptPo record = new AutoReceiveOptPo();
			record.setOrderId(orderId);
			record.setExpectReceiveTime(DateTime.now().plusDays(-1).toDate());
			record.setFreeze(false);

			//修改预期自动确认收货
			orderShardingWapper.autoReceiveOptMapper
					.updateByPrimaryKeySelective(record);

			Logger.comment("前置数据准备结束");

			int delayDay = 1;
			delayReceiveReq.setDelayDay(delayDay);
			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(-1);
			delayReceiveReq.setOperateUserType(2);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();

			verify(record.getExpectReceiveTime());

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_003() {
		Logger.start(false, "此订单不存在");
		try {
			int orderId = 1111;
			int sellerId = 288484;
			int delayDay = 1;
			delayReceiveReq.setDelayDay(delayDay);
			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11,
					delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_004() {
		Logger.start(true, "当前订单还没有超过可再次延期的时间限制");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			//贝海发货
			TradingCallService.delivery(sellerId, orderId, true);

			//自动确认收货时间操作表（auto_receive_opt） 
			AutoReceiveOptPo record = new AutoReceiveOptPo();
			record.setOrderId(orderId);
			record.setExpectReceiveTime(DateTime.now().plusDays(-1).toDate());
			record.setFreeze(false);

			//修改预期自动确认收货
			orderShardingWapper.autoReceiveOptMapper
					.updateByPrimaryKeySelective(record);

			Logger.comment("前置数据准备结束");

			int delayDay = 1;
			delayReceiveReq.setDelayDay(delayDay);
			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);
			delayReceiveReq.setOperateUserType(0);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();

			delayReceiveCall.callService();

			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyEquals(String.format("哈尼，每次延期操作间隔必须大于24小时", orderId),
					delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_005() {
		Logger.start(false, "订单号为null");
		try {
			int sellerId = 288484;
			int delayDay = 1;
			delayReceiveReq.setDelayDay(delayDay);
			//delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains(ECode.ERROR400_MSG_9,
					delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_006() {
		Logger.start(false, "延长的时间为0");
		try {
			int orderId = 1234;
			int sellerId = 288484;
			int delayDay = 0;
			delayReceiveReq.setDelayDay(delayDay);

			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains("必须大于0", delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_007() {
		Logger.start(false, "延长的时间小于0");
		try {
			int orderId = 1234;
			int sellerId = 288484;
			int delayDay = -1;
			delayReceiveReq.setDelayDay(delayDay);

			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains("必须大于0", delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_008() {
		Logger.start(false, "当前订单状态不是已发货,延长收货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			int delayDay = 1;
			delayReceiveReq.setDelayDay(delayDay);

			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains("未发货,不能做延长收货处理", delayReceiveCall.getMsg(),
					"验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_009() {
		Logger.start(false, "当前订单状订单冻结中,延长收货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingCallService
					.orderDelivery();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Logger.comment("前置数据准备结束");

			int delayDay = 1;
			delayReceiveReq.setDelayDay(delayDay);

			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(sellerId);

			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains(
					String.format("订单%s自动确认收货被冻结中,无需延长收货", orderId),
					delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DelayReceive_010() {
		Logger.start(true, "买家只能延长一次收货");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			long userId = placeOrderResp.getUserId();

			//贝海发货
			TradingCallService.delivery(sellerId, orderId, true);

			//自动确认收货时间操作表（auto_receive_opt） 
			AutoReceiveOptPo record = new AutoReceiveOptPo();
			record.setOrderId(orderId);
			record.setExpectReceiveTime(DateTime.now().plusDays(-1).toDate());
			record.setFreeze(false);

			//修改预期自动确认收货
			orderShardingWapper.autoReceiveOptMapper
					.updateByPrimaryKeySelective(record);

			Logger.comment("前置数据准备结束");

			int delayDay = 3;

			delayReceiveReq.setDelayDay(delayDay);
			delayReceiveReq.setOrderId(orderId);
			delayReceiveReq.setOperateUserId(userId);

			delayReceiveReq.setOperateUserType(1);
			// 发送请求
			delayReceiveCall.setData(delayReceiveReq);
			delayReceiveCall.callService();
			delayReceiveCall.callService();

			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");

			Logger.verifyEquals("105", delayReceiveCall.getBCode(), "验证Code");
			Logger.verifyIsContains(String.format("哈尼，%s此单已延长过收货时间", orderId),
					delayReceiveCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}